package com.study.juc.secstudy.threadpool;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.stream.IntStream;

public class RunnableQueue<T> {


    private LinkedList<T> queue = new LinkedList<T>();


    private final int maxSize;
    private int length ;


    private static final int DEFAULT_MAX_SIZE = 10 ;

    public RunnableQueue() {
        this.maxSize = DEFAULT_MAX_SIZE;
    }
    public RunnableQueue(int maxSize) {
        this.maxSize = maxSize;
    }

    public void offer(T t){
        if(t != null){
            synchronized (queue){
                while(length >=maxSize){
                    try {
                        queue.wait();
                    } catch (InterruptedException e) {
                    }
                }
                length ++;
                queue.addLast(t);
                queue.notifyAll();
            }
        }
    }


    public T take(){
        synchronized (queue){
            while(length <= 0){
                try {
                    queue.wait();
                } catch (InterruptedException e) {
                }
            }
            T t = queue.removeFirst();
            length--;
            queue.notifyAll();
            return t;

        }
    }

    public static void main(String[] args) {

        IntStream.range(0,500);

    }
}
